﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using Microsoft.Win32;
using Shell32;

namespace com.his.mediaFileOrganizer
{
    class FileAnalyzer
    {
        string targetFolder;

        public FileAnalyzer(string targetFolder) { this.targetFolder = targetFolder; }

        public void Analyze(String folder)
        {
            try
            {
                Shell32.Shell shell = new Shell32.Shell();
                Shell32.Folder objFolder;

                objFolder = shell.NameSpace(folder);

                foreach (Shell32.FolderItem2 item in objFolder.Items())
                {
                    try
                    {
                        if (!item.IsFolder)
                        {
                            MediaProperties mp = new MediaProperties(ref objFolder, item);

                            // Move file to target location
                            string targetPath = targetFolder + "\\" + mp.TargetFolderPath;
                            if (!Directory.Exists(targetPath))
                            {
                                Directory.CreateDirectory(targetPath);
                            }

                            File.Move(mp.Path, targetPath + "\\" + mp.Name);

                            Console.WriteLine(DateTime.Now.ToString() + "\t{0}\t{1}\t{2}\t{3}", mp.Name, mp.CreatedOn, mp.TakenOn, targetPath);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }

                }

                foreach (DirectoryInfo di in new DirectoryInfo(folder).GetDirectories())
                {
                    Analyze(di.FullName);
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

        }

    }
}
